/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.sdgakj.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.google.common.collect.ImmutableList;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.hibernate.validator.constraints.NotBlank;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Entity
@Table(name = "f_user")
@JsonIgnoreProperties({ "handler","hibernateLazyInitializer" })
public class User extends IdEntity implements Serializable {

    private static final long serialVersionUID = 1L;
    private String loginName;
    private String plainPassword;
    private String password;
    private String name;
    private String salt;
    private String mobile;
    private String type; //代表当前是管理员还是普通用户
    private Date registerDate;
    private String deltag;
    private Role role;
    /**省*/
    private ShtProvince shtProvince;
    /**市*/
    private ShtCity shtCity;
    /**县区*/
    private ShtDistrict shtDistrict;
    private String status;
    private String sex ="0";
    private Integer point;
    private Integer integration;
    private Person person;
    private String openid;
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="fp_id")
    @JsonIgnore
    public Person getPerson() {
        return person;
    }
    public void setPerson(Person person) {
        this.person = person;
    }
    //    @Column(name = "fp_id")
//    private Long personid;//个人信息

    private Long familyid;//所属家族

    @Column(name = "f_id")
    public Long getFamilyid() {
        return familyid;
    }

    public void setFamilyid(Long familyid) {
        this.familyid = familyid;
    }
//    public Long getPersonid() {
//        return personid;
//    }
//    public void setPersonid(Long personid) {
//        this.personid = personid;
//    }

    public User() {
    }

    public User(Long id) {
        this.id = id;
    }

    @Column
    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    @Column
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Column
    public String getDeltag() {
        return deltag;
    }

    public void setDeltag(String deltag) {
        this.deltag = deltag;
    }



    @NotBlank
    @Column(name = "loginname")
    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    @Transient
    @JsonIgnore
    public List<String> getTypeList() {
        // 角色列表在数据库中实际以逗号分隔字符串存储，因此返回不能修改的List.
        return ImmutableList.copyOf(StringUtils.split(type, ","));
    }

    public String getOpenid() {
        return openid;
    }

    public void setOpenid(String openid) {
        this.openid = openid;
    }

    @NotBlank
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    // 不持久化到数据库，也不显示在Restful接口的属性.
    @Transient
    @JsonIgnore
    public String getPlainPassword() {
        return plainPassword;
    }

    public void setPlainPassword(String plainPassword) {
        this.plainPassword = plainPassword;
    }

    @Column
    @JsonIgnore
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Column
    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

//	@Transient
//	@JsonIgnore
//	public List<String> getRoleList() {
//		// 角色列表在数据库中实际以逗号分隔字符串存储，因此返回不能修改的List.
//		return ImmutableList.copyOf(StringUtils.split(roles, ","));
//	}

    // 设定JSON序列化时的日期格式
    @Column(name = "register_date")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08:00")
    public Date getRegisterDate() {
        return registerDate;
    }

    public void setRegisterDate(Date registerDate) {
        this.registerDate = registerDate;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "roleid")
    @JsonIgnore
    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @ManyToOne
    @JoinColumn(name="province_id")
    public ShtProvince getShtProvince() {
        return shtProvince;
    }

    public void setShtProvince(ShtProvince shtProvince) {
        this.shtProvince = shtProvince;
    }

    @Transient
    public String getProvinceName(){
        if(shtProvince!=null){
            return shtProvince.getName();
        }else{
            return "";
        }
    }

    @ManyToOne
    @JoinColumn(name="city_id")
    public ShtCity getShtCity() {
        return shtCity;
    }

    public void setShtCity(ShtCity shtCity) {
        this.shtCity = shtCity;
    }

    @Column
    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Integer getIntegration() {
        return integration;
    }

    public void setIntegration(Integer integration) {
        this.integration = integration;
    }
    @Column(name = "point")
    public Integer getPoint(){return  point;}

    public void  setPoint(Integer point){
        this.point=point;
    }
   /* @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }*/

}
